package avicit.bdp.dcs.utils;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2021-01-28 18:09
 * @类说明：
 * @修改记录：
 */
public class PropertyUtil {

    private static final Properties properties = new Properties();

    private PropertyUtil() {
        throw new IllegalStateException("PropertyUtil class");
    }

    static {
        String[] propertyFiles = new String[]{"classpath*:hive.properties"};
        for (String fileName : propertyFiles) {
            InputStream fis = null;
            try {
                ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
                Resource[] resources = resourcePatternResolver.getResources(fileName);
                // 如果是空的，则不执行
                if (resources.length != 0) {
                    for(Resource resource : resources){
                        fis = resource.getInputStream();
                        properties.load(fis);
                    }
                }
            } catch (IOException e) {
                if (fis != null) {
                    IOUtils.closeQuietly(fis);
                }
                System.exit(1);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }
    }

    /**
     * get property value
     *
     * @param key property name
     * @return property value
     */
    public static String getString(String key) {
        return properties.getProperty(key.trim());
    }

}
